folders = ["Domination-Game", "Domination-Game/domination/libs", "Domination-Game/domination", "awesome","maarten"]

import os, sys
# add the folders with our awesome modules
for folder in folders:
    sys.path.append("%s%s%s" % (os.getcwd(), os.sep, folder))

from utilities import *
from libs import *


import pickle
import math
from optparse import OptionParser
from domination import core, scenarios


FIELD = """
w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w
w _ _ _ _ _ _ _ w _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ w
w _ _ _ _ _ _ _ w _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ w
w _ _ _ _ _ _ _ w _ _ _ _ _ C _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ w
w _ _ _ _ _ _ _ w _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ w
w _ _ _ _ _ _ _ w w w w w w w w w w w w w w w _ _ _ _ _ _ _ w
w _ _ _ w _ _ _ w _ _ _ _ _ _ _ _ _ _ a _ _ _ _ _ _ w _ w w w
w _ _ _ w _ _ _ w _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ w _ w B w
w R _ _ w _ _ _ w _ _ _ _ w w w w w _ _ _ _ w _ _ _ w _ w B w
w _ _ _ w _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ w _ _ _ w _ w B w
w _ _ _ w _ _ _ _ _ _ a _ _ _ _ _ _ _ _ _ _ w _ _ _ w _ w w w
w _ _ _ _ _ _ _ w w w w w w w w w w w w w w w _ _ _ _ _ _ _ w
w _ _ _ _ _ _ _ w _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ w
w _ _ _ _ _ _ _ w _ _ _ _ _ _ _ C _ _ _ _ _ _ _ _ _ _ _ _ _ w
w _ _ _ _ _ _ _ w _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ w
w _ _ _ _ _ _ _ w _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ w
w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w
"""

class Tournament1(scenarios.Scenario):
    GENERATOR = None
    FIELD     = core.Field.from_string(FIELD)
    SETTINGS  = core.Settings(max_turn=0.78539816339744828, think_time=0.059999999999999998, ammo_rate=9, spawn_time=11, ammo_amount=1, max_steps=300, max_score=2, max_see=80)

def parse_arguments():
    """ Parses the command line arguments, stores the default values and
    returns a settings reference in which all the settings are stored. """
    default_agent = "Domination-Game/domination/agent.py"

    parser = OptionParser(description="""Launch the domination game with
command line arguments. If agents are not specified, %s is used. """ % default_agent)
    parser.add_option("-r", "--red-agent", dest="RED_AGENT",
                    help="""Sets a path for the red agent""",
                    metavar="RED_AGENT",
                    default=default_agent)
    parser.add_option("-b", "--blue-agent", dest="BLUE_AGENT",
                    help="""Sets a path for the blue agent""",
                    metavar="BLUE_AGENT",
                    default=default_agent)
    parser.add_option("-p", "--pickle", dest="PICKLE",
                    help="""Loads a pickle as a replay and plays the shit out of it!""",
                    metavar="PICKLE",
                    default=None)
    parser.add_option("-t", "--test", dest="TEST", default=False,
                    help="""Set the script to test the bot in a scenario similar to the server...""", action="store_true")


    (settings, args) = parser.parse_args()
    return settings


def test(settings):
    """ Runs a test for bots that wanna go live on the server """
    field = core.Field.from_string(FIELD)
    core_settings = core.Settings(think_time=0.0625, ammo_rate=9,
                        ammo_amount=1, max_score=100, max_see=70)
    game = core.Game(rendered=True, red=open(settings.RED_AGENT).read(),
                    blue=open(settings.BLUE_AGENT).read(),
                    settings=core_settings, field=field)
    game.run()


def base(settings):
    """ Runs the scenario that was supplied in ataa_2013_1.py """
    # Tournament1.one_on_one(red="domination/agent.py", blue="domination/agent.py", output_folder='_tmp')
    Tournament1.test(red=settings.RED_AGENT, blue=settings.BLUE_AGENT)

def replay(settings):
    """ Plays a pickle replay file """
    rp = pickle.load(open(settings.PICKLE,'rb'))
    rp.play()

if __name__ == "__main__":
    settings = parse_arguments()


    if settings.TEST:
        print "Running a test for bots that wanna go live on the server in an attempt to mimick the crazy test server"
        test(settings)
    elif settings.PICKLE == None:
        print "Running the scenario as supplied in ataa_2013_1.py (default)"
        base(settings)
    else:
        print "Playing a pickle replay file"
        replay(settings)
